<div class="bg-light lter b-b wrapper-md">
  <h1 class="m-n font-thin h3">节目管理</h1>
</div>

<div class="wrapper-md"  >
  <div class="panel panel-default">
    <div ng-controller="infoEditCtrl">
      <div class="row">
        <div class="col-sm-12" >
          <form name="form-contact" ng-submit="processForm()" class="form-horizontal">
            <div class="panel-heading">
              <span class="h4">
                <button class="btn btn-default btn-sm " ng-click="goBack()"><i class="fa fa-mail-reply fa-lg"></i>&nbsp; 返回</button>
              </span>
            </div>
              <div class="panel-body">
                <div class="panel-heading">
                  <span class="h4" style="color: blue;">影视简介信息</span>
                </div>
                <div class="form-group">
                  <label class="col-sm-1 control-label">直播流分类</label>
                    <div class="col-sm-5">
                      <ui-select multiple ng-model="multipleDemo.selectedPeopleWithGroupBy" theme="bootstrap" ng-disabled="disabled">
                        <ui-select-match placeholder="所属分类">{{$item.ClassName}} &lt;{{$item.ClassRemark}}&gt;</ui-select-match>
                         
                        <ui-select-choices  repeat="person in people | propsFilter: {ClassName: $select.search, Id: $select.search}">
                        <div ng-bind-html="person.ClassName | highlight: $select.search"></div>
                        <small>  
                          编号: <span ng-bind-html="''+person.Id | highlight: $select.search"></span>
                        </small>
                        </ui-select-choices>
                      </ui-select>
                    </div>
                  </div>
                
                  <div class="form-group">
                    <label class="col-sm-1 control-label">标题</label>
                    <div class="col-sm-5">
                      <input type="text" class="form-control" placeholder="少一个一句话介绍自己" ng-model="contact.ProgramTitle" required >
                    </div>
                    <label  class="col-sm-1 control-label">英文名</label>
                    <div class="col-sm-5">
                      <input type="text" class="form-control" placeholder="英文名" ng-model="contact.eProgramTitle"  >
                    </div>
                  </div>
                  <div class="line line-dashed b-b line-lg pull-in"></div>

                  <div class="form-group">
                    <label class="col-sm-1 control-label">制片人</label>
                    <div class="col-sm-5">
                    <input type="text" class="form-control" placeholder="制片人" ng-model="contact.ProgramAuthor" required >
                    </div>
                    <label  class="col-sm-1 control-label">导演</label>
                    <div class="col-sm-5">
                    <input type="text" class="form-control" placeholder="导演" ng-model="contact.director" required >
                    </div>
                  </div>
                <div class="line line-dashed b-b line-lg pull-in"></div>
             
                 <div class="form-group">
                    <label class="col-sm-1 control-label">编剧</label>
                    <div class="col-sm-5">
                    <input type="text" class="form-control" placeholder="编剧" ng-model="contact.screenwriter" required >
                    </div>
                    <label  class="col-sm-1 control-label">片长</label>
                    <div class="col-sm-5">
                      <input type="text" class="form-control" placeholder="片长" ng-model="contact.filmLength" required >
                    </div>
                  </div>
                <div class="line line-dashed b-b line-lg pull-in"></div>

                 <div class="form-group">
                  <label class="col-sm-1 control-label">制片地区</label>
                  <div class="col-sm-5">
                  <input type="text" class="form-control" placeholder="制片地区" ng-model="contact.productArea" required >
                  </div>
                  <label  class="col-sm-1 control-label">出品公司</label>
                  <div class="col-sm-5">
                    <input type="text" class="form-control"   ng-model="contact.productCompany" required >
                  </div>
                 </div>
                <div class="line line-dashed b-b line-lg pull-in"></div>

                  <div class="form-group">
                    <label class="col-sm-1 control-label">出品时间</label>
                    <div class="col-sm-2">
                    <input type="text"  ng-model="contact.productTime" id="productTime"   onFocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="form-control" />
                    </div>
                    <label  class="col-sm-1 control-label">上映时间</label>
                    <div class="col-sm-2">
                    <input type="text"  ng-model="contact.releaseTime" id="releaseTime"   onFocus="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="form-control" />
                    </div>
                   <label class="col-sm-1 control-label">有效时间</label>
                    <div class="col-sm-2">

                    <input type="text"  ng-model="contact.ValidityBegin" id="validityBegin"  onFocus="WdatePicker({maxDate:'#F{$dp.$D(\'validityEnd\')||\'2020-10-01\'}',dateFmt:'yyyy-MM-dd'})"  class="form-control" />
                    </div>
                    <div class="col-sm-2">
                    <input type="text"  ng-model="contact.ValidityEnd" id="validityEnd"   onFocus="WdatePicker({minDate:'#F{$dp.$D(\'validityBegin\')}',maxDate:'2020-10-01',dateFmt:'yyyy-MM-dd'})" class="form-control" />
                    </div>
                  </div>
                <div class="line line-dashed b-b line-lg pull-in"></div>

                  <div class="form-group">
                    <label class="col-sm-1 control-label">类型</label>
                    <div class="col-sm-5">
                    <input type="text" class="form-control" placeholder="类型" ng-model="contact.type" required >
                    </div>
                    <label  class="col-sm-1 control-label">标签</label>
                    <div class="col-sm-5">
                    <input type="text" class="form-control" placeholder="标签" ng-model="contact.ProgramLabel" required >
                    </div>
                  </div>
                <div class="line line-dashed b-b line-lg pull-in"></div>

                  <div class="form-group">
                     <label class="col-sm-1 control-label">节目简介</label>  
                   <div class="col-sm-10">
                      <textarea class="form-control" rows="6" ng-model="contact.ProgramIntroduction" placeholder="节目简介"></textarea>
                   </div>
                  </div>
                <div class="line line-dashed b-b line-lg pull-in"></div>


                <div class="form-group">
                  <label class="col-sm-1 control-label">海报（横）:</label>  
                  <div class="col-sm-5">
                    <img src="" name="ims" id="imgData" onerror="javascript:this.src='img/common/300x300.png';" style="height: 90px;text-align: center;line-height: 90px;" alt="图标预览">
                    <br/>
                    <span id="browsebtn">
                    <input type="file" name="File" id="File" />
                    </span> 
                  </div>

                  <label class="col-sm-1 control-label">海报（竖）:</label>  
                  <div class="col-sm-5">
                  <img src="" name="ims" id="imgData" onerror="javascript:this.src='img/common/300x300.png';" style="height: 90px;text-align: center;line-height: 90px;" alt="图标预览">
                    <span id="browsebtn">
                    <input type="file" name="File" id="File" />
                    </span>
                    <input type="button" value="验证" onclick="readLocalFile()"/>
                    <input type="button" value="确认" onclick="forUpload()"/>
                  </div>
                </div>
              <div class="line line-dashed b-b line-lg pull-in"></div>


    <!-- nav -->
    <nav ui-nav class="navi" ng-include="'src/app/program/contains_diversity.html'"></nav>
    <!-- nav -->

 <!-- nav -->
    <nav ui-nav class="navi" ng-include="'src/app/program/contains_comment.html'"></nav>
    <!-- nav -->


        

        <div class="panel-body">
          <div class="panel-heading">
            <span class="h4" style="color: blue;">影视配置信息</span>
          </div>
          <div class="form-group">
            <label class="col-sm-1 control-label">播放次数</label>
            <div class="col-sm-5">
            <input type="Number" class="form-control"  ng-model="contact.ProgramPlayCount" required >
            </div>

          <label  class="col-sm-1 control-label">排序</label>
              <div class="col-sm-5">
                <input type="Number" class="form-control"  min="0" max="99"    ng-model="contact.ProgramDisplayOrder" required >
              </div>
          </div>
        <div class="line line-dashed b-b line-lg pull-in"></div>

          <div class="form-group">
            <label class="col-sm-1 control-label">审核时间</label>
            <div class="col-sm-5">
            <input type="text"  ng-model="contact.AuditTime" id="auditTime"  onClick="WdatePicker({dateFmt:'yyyy-MM-dd'})" class="form-control" />
            </div>
            <label  class="col-sm-1 control-label">审核人</label>
            <div class="col-sm-5">
            <input type="Number" class="form-control"   ng-model="contact.AuditorId" required >
            </div>
          </div>
        <div class="line line-dashed b-b line-lg pull-in"></div>
         <div class="form-group">
            <label class="col-sm-1 control-label">是否审核</label>
            <div class="col-sm-1">
            <label class="i-switch m-t-xs m-r">
            <input type="checkbox"   ng-model="programIsAudited"  checked>
              <i></i>
            </label>
            </div>
            <label class="col-sm-1 control-label">PC</label>
            <div class="col-sm-1">
            <label class="i-switch m-t-xs m-r">
            <input type="checkbox"  ng-model="programDisplayOnPc"  checked>
            <i></i>
            </label>
            </div>
            <label class="col-sm-1 control-label">Phone</label>
            <div class="col-sm-1">
            <label class="i-switch m-t-xs m-r">
            <input type="checkbox"  ng-model="programDisplayOnPhone"  checked>
            <i></i>
            </label>
            </div>
            <label class="col-sm-1 control-label">Pad</label>
            <div class="col-sm-1">
            <label class="i-switch m-t-xs m-r">
            <input type="checkbox"  ng-model="programDisplayOnPad"  checked>
            <i></i>
            </label>
            </div>
            <label class="col-sm-1 control-label">TV</label>
            <div class="col-sm-1">
            <label class="i-switch m-t-xs m-r">
            <input type="checkbox"  ng-model="programDisplayOnTv"  checked>
            <i></i>
            </label>
            </div>
          </div>
        <div class="line line-dashed b-b line-lg pull-in"></div>
             </div>
             <footer class="panel-footer text-right bg-light lter">
             <button type="submit" id="start" class="btn btn-success">提交</button>
             </footer>
            </div>
          </form>
        </div>
      </div>
    </div>
  </div>
</div>


<!--angularjs-->
<script type="text/javascript">


var imageIdOne;
var imageIdTwo;

app.controller('infoEditCtrl', function ($scope, $state, $stateParams,$http,$window) {

  $scope.programDisplayOnPc = true;
  $scope.programDisplayOnPhone = true;
  $scope.programDisplayOnPad = true;
  $scope.programDisplayOnTv = true;
  $scope.programIsAudited = true;
  $scope.programIsDisplay = true;

//返回按钮：开始
$scope.$window = $window;
  $scope.goBack = function(){
    $window.history.back();
  };
//返回按钮：结束

  //修改数据 跳转带Id
  var infoId = $stateParams.infoId;

 //如果infoId不为空，查询详情：开始
  if(''!=infoId){
      var getInfoUrl = API_URL+"program_info?query=delflag:0,id:"+infoId;    

    $http.get(getInfoUrl).success(function (response) {

         var obj = response[0];

          obj.ValidityBegin = getDate(obj.ValidityBegin);

          obj.ValidityEnd = getDate(obj.ValidityEnd);
          obj.AuditTime = getDate(obj.AuditTime);

           //从编目里取信息
          var ProgramCatalog = obj.ProgramCatalog;

          console.log("ProgramCatalog"+ProgramCatalog);
          //此处是报错点 暂时 不明白
          if(''!=ProgramCatalog){
           ProgramCatalog =  JSON.parse(ProgramCatalog);

            if("undefined"!=typeof(ProgramCatalog.eProgramTitle)){
              obj.eProgramTitle = ProgramCatalog.eProgramTitle;
            }
                if("undefined"!=typeof(ProgramCatalog.director)){
                   obj.director = ProgramCatalog.director;
            }
                if("undefined"!=typeof(ProgramCatalog.toStar)){
                 obj.toStar = ProgramCatalog.toStar;
            }
                if("undefined"!=typeof(ProgramCatalog.screenwriter)){
              obj.screenwriter = ProgramCatalog.screenwriter;
            }
                if("undefined"!=typeof(ProgramCatalog.type)){
               obj.type = ProgramCatalog.type;
            }
        
                if("undefined"!=typeof(ProgramCatalog.productTime)){
              obj.productTime = ProgramCatalog.productTime;
            }
                if("undefined"!=typeof(ProgramCatalog.filmLength)){
              obj.filmLength = ProgramCatalog.filmLength;
            }
                if("undefined"!=typeof(ProgramCatalog.productCompany)){
              obj.productCompany = ProgramCatalog.productCompany;
            }
                if("undefined"!=typeof(ProgramCatalog.productArea)){
              obj.productArea = ProgramCatalog.productArea;
            }
                if("undefined"!=typeof(ProgramCatalog.releaseTime)){
              obj.releaseTime = ProgramCatalog.releaseTime;
            }
               

          }

          obj.ProgramDisplayOrder = Number(obj.ProgramDisplayOrder);

       if("0" == obj.ProgramDisplayOnPc){
           $scope.programDisplayOnPc = false;
           }
           if("0" == obj.ProgramDisplayOnPhone){
            $scope.programDisplayOnPhone = false;
           }
           if("0" == obj.ProgramDisplayOnPad){
           $scope.programDisplayOnPad = false;
           }
           if("0" == obj.ProgramDisplayOnTv){
           $scope.programDisplayOnTv = false;
           }
           if("0" == obj.ProgramIsAudited){
           $scope.programIsAudited = false;
           }
           if("0" == obj.ProgramIsDisplay){
           $scope.programIsDisplay = false;
           }

           $scope.contact = obj;
      });
  }
//如果infoId不为空，查询详情：结束


  //表单提交开始
$scope.processForm= function (){

 //1.参数处理

//发起POST请求
var postInfoUrl = API_URL+"program_info";
var formData =  $scope.contact;

  //1.参数处理
    formData.AddTime = getNowDBFormatDate();
    formData.UpdateTime = getNowDBFormatDate();
    formData.AdderId = 1;
    formData.AdderName = '1';
    formData.UpdaterId = 1;
    formData.UpdaterName = '1';
    formData.ProgramUuid='';

    formData.ProgramDisplayOnPc = '1';
    formData.ProgramDisplayOnPad = '1';
    formData.ProgramDisplayOnPhone = '1';
    formData.ProgramDisplayOnTv = '1';
    formData.ProgramIsAudited = '1';
    formData.ProgramIsDisplay = '1';
    formData.DelFlag='0';
    if(false == $scope.programDisplayOnPc){
          formData.ProgramDisplayOnPc = '0';
         }
    if(false == $scope.programDisplayOnPad){
          formData.ProgramDisplayOnPad = '0';
         }

    if(false == $scope.programDisplayOnPhone){
          formData.ProgramDisplayOnPhone = '0';
         }
    if(false == $scope.programDisplayOnTv){
          formData.ProgramDisplayOnTv = '0';
         }

    if(false == $scope.programIsAudited){
          formData.ProgramIsAudited = '0';
         }
    if(false == $scope.programIsDisplay){
          formData.ProgramIsDisplay = '0';
         }

         formData.ProgramDisplayOrder = formData.ProgramDisplayOrder+"";
         var validityBegin = $('#validityBegin').val();

         formData.ValidityBegin = forDbDateTime(validityBegin);
         
         var validityEnd = $('#validityEnd').val();
         formData.ValidityEnd = forDbDateTime(validityEnd);
        
         var auditTime = $('#auditTime').val();
         formData.AuditTime = forDbDateTime(auditTime);

         formData.releaseTime = $('#releaseTime').val();

         formData.productTime = $('#productTime').val();


         //json 数据
        var ProgramCatalog = {};
        //英文名
        ProgramCatalog.eProgramTitle = formData.eProgramTitle;
        //导演
        ProgramCatalog.director = formData.director;
        //
        ProgramCatalog.toStar = formData.toStar;
        //编剧
        ProgramCatalog.screenwriter = formData.screenwriter;
        //类型
        ProgramCatalog.type = formData.type;
        //时长
        ProgramCatalog.filmLength = formData.filmLength;
        //出品时间
        ProgramCatalog.productTime = formData.productTime;
        //出品公司
        ProgramCatalog.productCompany = formData.productCompany;
        //制片地区
        ProgramCatalog.productArea = formData.productArea;
        //上映时间
        ProgramCatalog.releaseTime = formData.releaseTime;

        formData.ProgramCatalog = JSON.stringify(ProgramCatalog);



  if("undefined"!=typeof(formData.Id)){
    var programId = Number(infoId);
          $.ajax( {  
                  async : false, //是否异步  
                  cache : false, //是否使用缓存  
                  type : 'put', //请求方式,post  
                  dataType : "json", //数据传输格式  
                  url : postInfoUrl+'/'+infoId, //请求链接  
                  data: JSON.stringify(formData),
                  error : function() {  
                      console.log('亲，网络有点不给力呀！');  
                  },  
                  success : function(data) { 
                    var url =  API_URL+"r_program_2_class";
                    var objs = MY_ASYNC_AJAX(url+"?query=delflag:0,ProgramId:"+programId);
                 
                    if(objs!=null&&objs.length>0){
                    for(var i =0; i<objs.length;i++){
                    $.ajax({  
                          async : false, //是否异步  
                          cache : false, //是否使用缓存  
                          type : 'delete', //请求方式,post  
                          url : url+'/'+objs[i].Id, //请求链接  
                          error : function() {  
                              console.log('亲，网络有点不给力呀！');  
                          },  
                          success : function(data) {
                       
                     
                          }  
                     }); 

                    }
                  }
                       var classArr =  $scope.multipleDemo.selectedPeopleWithGroupBy;
                      
                    if(''!= classArr){

                      for(var i =0;i<classArr.length;i++){
                         var pushClassUrl =  API_URL+"r_program_2_class";
                          var classFormData = {};
                             classFormData.AddTime = getNowDBFormatDate();
                             classFormData.AdderId = 1;
                             classFormData.AdderName = '1';
                             classFormData.delflag = '0';
                             classFormData.ProgramId = programId;
                             classFormData.ClassId = classArr[i].Id;
                               $.ajax( {  
                                async : false, //是否异步  
                                cache : false, //是否使用缓存  
                                type : 'post', //请求方式,post  
                                dataType : "json", //数据传输格式  
                                url : pushClassUrl, //请求链接  
                                data: JSON.stringify(classFormData),
                                error : function() {  
                                    console.log('亲，网络有点不给力呀！');  
                                },  
                                success : function(data) { 
                               
                                }  
                           }); 
                        
                      }
                    }
                    top.layer.msg('保存成功', {icon: 6});
                 //2.成功跳转页面
                 $state.go('app.movie.program');
                  }  
             }); 


  }else{

     $.ajax( {  
        async : false, //是否异步  
        cache : false, //是否使用缓存  
        type : 'post', //请求方式,post  
        dataType : "json", //数据传输格式  
        url : postInfoUrl, //请求链接  
        data: JSON.stringify(formData),
        error : function() {  
            console.log('亲，网络有点不给力呀！');  
        },  
        success : function(data) { 
           var classArr =  $scope.multipleDemo.selectedPeopleWithGroupBy;
           console.log(classArr);
          if(''!= classArr){
            for(var i =0;i<classArr.length;i++){
              var pushClassUrl =  API_URL+"r_program_2_class";
              var exists = MY_ASYNC_AJAX(pushClassUrl+"?query=delflag:0,ProgramId:"+data[0].Id+",ClassId:"+classArr[i].Id);
              if(null==exists){
                var classFormData = {};
                   classFormData.AddTime = getNowDBFormatDate();
                   classFormData.AdderId = 1;
                   classFormData.AdderName = '1';
                   classFormData.delflag = '0';
                   classFormData.ProgramId = data[0].Id;
                   classFormData.ClassId = classArr[i].Id;

                     $.ajax( {  
                      async : false, //是否异步  
                      cache : false, //是否使用缓存  
                      type : 'post', //请求方式,post  
                      dataType : "json", //数据传输格式  
                      url : pushClassUrl, //请求链接  
                      data: JSON.stringify(classFormData),
                      error : function() {  
                          console.log('亲，网络有点不给力呀！');  
                      },  
                      success : function(data) { 
                     
                      }  
                 }); 
              }
            }
          }
          top.layer.msg('保存成功', {icon: 6});
       //2.成功跳转页面
       $state.go('app.movie.program');
        }  
   }); 

  }


}
 //表单提交结束
 
});

app.filter('propsFilter', function() {
    return function(items, props) {
        var out = [];

        if (angular.isArray(items)) {
          items.forEach(function(item) {
            var itemMatches = false;

            var keys = Object.keys(props);
            for (var i = 0; i < keys.length; i++) {
              var prop = keys[i];
              var text = props[prop].toLowerCase();
              if (item[prop].toString().toLowerCase().indexOf(text) !== -1) {
                itemMatches = true;
                break;
              }
            }

            if (itemMatches) {
              out.push(item);
            }
          });
        } else {
          // Let the output be the input untouched
          out = items;
        }

        return out;
    };
})


//选择类型下拉框
app.controller('SelectCtrl', function($scope, $http, $stateParams,$timeout) {



        $scope.disabled = undefined;
        $scope.searchEnabled = undefined;

        $scope.enable = function() {
        $scope.disabled = false;
        };

        $scope.disable = function() {
        $scope.disabled = true;
        };

        $scope.enableSearch = function() {
        $scope.searchEnabled = true;
        }

        $scope.disableSearch = function() {
        $scope.searchEnabled = false;
        }

        $scope.clear = function() {
        $scope.person.selected = undefined;
        $scope.address.selected = undefined;
        $scope.country.selected = undefined;
        };


        $scope.personAsync = {selected : "wladimir@email.com"};
        $scope.peopleAsync = [];


        $scope.counter = 0;
        $scope.someFunction = function (item, model){
        $scope.counter++;
        $scope.eventResult = {item: item, model: model};
        };

        $scope.removed = function (item, model) {
        $scope.lastRemoved = {
            item: item,
            model: model
        };
        };

        $scope.person = {};

        var infoUrl = API_URL+"program_class?query=delflag:0";

        var formData =  MY_ASYNC_AJAX(infoUrl);
        $scope.people = formData;
        $scope.multipleDemo = {};
        $scope.multipleDemo.colors = ['Blue','Red'];

        //已经存在的
        var infoId = $stateParams.infoId;
         $scope.multipleDemo.selectedPeopleWithGroupBy = [];

         if(''!=infoId&&infoId!=undefined){
          
            var classUrl = API_URL+"r_program_2_class?query=delflag:0,ProgramId:"+infoId;
            var classList = MY_ASYNC_AJAX(classUrl);
             console.log('获取classList：'+classList);
          if(null !=  classList){
            for(var i= 0;i<classList.length;i++){
                     var infoClassUrl = API_URL+"program_class?query=delflag:0,Id:"+classList[i].ClassId;
                     var infoClass = MY_ASYNC_AJAX(infoClassUrl)[0];
                        for(var j=0;j<formData.length;j++){
                        if($scope.people[j].Id == infoClass.Id){
                          $scope.multipleDemo.selectedPeopleWithGroupBy.push($scope.people[j]);
                          break;
                        }
                     }
                      
              }
            }
        }else{
          var classId = $stateParams.classId;
          if(classId!=''&&classId!=undefined){
            for(var j=0;j<formData.length;j++){
              if($scope.people[j].Id == classId){

                $scope.multipleDemo.selectedPeopleWithGroupBy.push($scope.people[j]);
               
                }
            }
          }

        }

      
});

</script>
<!--angularjs-->











